﻿using System;
using UnityEngine;
using UnityEngine.AI;
using Utils;

namespace Core
{
    public class UnitMovementStop : MonoBehaviour, IAwaitable<AsyncExtensions.Void>
    {
        public event Action OnStop;

        [SerializeField] private NavMeshAgent _agent;

        private void Update()
        {
            if (_agent.pathPending) return;

            if (_agent.remainingDistance > _agent.stoppingDistance) return;

            if (!_agent.hasPath || _agent.velocity.sqrMagnitude == 0f)
            {
                OnStop?.Invoke();
            }
        }

        public IAwaiter<AsyncExtensions.Void> GetAwaiter() => new StopAwaiter(this);

    }
}